<?xml version="1.0" encoding="UTF-8"?>
<project name="popcornhour-controller" default="start" basedir=".">
    <property name="sources" value="${basedir}/src"/>
    <property name="approot" value="${basedir}/war"/>
    <property name="webroot" value="${approot}/WEB-INF"/>
    <property name="appbinaries" value="${webroot}/classes"/>
    <property name="applibraries" value="${webroot}/lib"/>

    <property name="module" value="${ant.project.name}"/>
    <property name="jetty" value="org.dyndns.merelin.pchrc.appserver.Jetty"/>

    <fileset id="rtlibraries" dir="${applibraries}">
        <include name="aopalliance.jar"/>
        <include name="guice-2.0.jar"/>
        <include name="gwt-servlet.jar"/>
        <include name="jetty-6.1.24.jar"/>
        <include name="jetty-util-6.1.24.jar"/>
        <include name="jmdns-3.4.1.jar"/>
        <include name="servlet-api-2.5.jar"/>
    </fileset>

    <fileset id="devlibraries" dir="${applibraries}">
        <include name="gwt-dev.jar"/>
        <include name="gwt-user.jar"/>
        <include name="validation-api-1.0.0.GA.jar"/>
        <include name="validation-api-1.0.0.GA-sources.jar"/>
    </fileset>

    <target name="clean">
        <delete dir="${appbinaries}" failonerror="false"/>
        <delete file="${applibraries}/${module}.jar" failonerror="false"/>
        <delete file="${basedir}/${module}.war" failonerror="false"/>
    </target>

    <target name="build" depends="clean">
        <mkdir dir="${appbinaries}"/>
        <javac destdir="${appbinaries}" debug="on">
            <src path="${sources}"/>
            <include name="*/**"/>
            <classpath>
                <path>
                    <fileset refid="rtlibraries"/>
                </path>
            </classpath>
        </javac>
    </target>

    <target name="pack" depends="gwtc">
        <unjar dest="${appbinaries}">
            <fileset refid="rtlibraries"/>
            <patternset>
                <exclude name="META-INF/**/*"/>
            </patternset>
        </unjar>
        <jar jarfile="${applibraries}/${module}.jar">
            <fileset dir="${appbinaries}" includes="**/*"/>
            <manifest>
                <attribute name="Main-Class" value="${jetty}"/>
            </manifest>
        </jar>
    </target>

    <target name="war" depends="pack">
        <war destfile="${basedir}/${module}.war" webxml="${webroot}/web.xml">
            <fileset dir="${appbinaries}" includes="Launcher.class"/>
            <lib file="${applibraries}/${module}.jar"/>
            <fileset dir="${approot}">
                <exclude name="WEB-INF/classes/**/*"/>
                <exclude name="WEB-INF/lib/**/*"/>
            </fileset>
            <manifest>
                <attribute name="Main-Class" value="Launcher"/>
            </manifest>
        </war>
    </target>

    <target name="start" depends="war">
        <java jar="${basedir}/${module}.war" fork="true">
            <sysproperty key="java.net.preferIPv4Stack" value="true"/>
            <sysproperty key="module.name" value="${module}"/>
            <sysproperty key="jetty.class" value="${jetty}"/>
            <!--sysproperty key="jetty.host" value="mantonyan"/-->
            <sysproperty key="jetty.port" value="9090"/>
        </java>
    </target>

    <target name="gwtc" depends="build">
        <java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">
            <classpath>
                <pathelement location="${sources}"/>
                <path>
                    <fileset refid="rtlibraries"/>
                    <fileset refid="devlibraries"/>
                </path>
            </classpath>
            <sysproperty key="java.net.preferIPv4Stack" value="true"/>
            <arg value="org.dyndns.merelin.pchrc.Popcornhour_controller"/>
        </java>
    </target>

    <target name="debug" depends="build">
        <java failonerror="true" fork="true" classname="com.google.gwt.dev.DevMode">
            <classpath>
                <pathelement location="${sources}"/>
                <path>
                    <fileset refid="rtlibraries"/>
                    <fileset refid="devlibraries"/>
                </path>
            </classpath>
            <sysproperty key="java.net.preferIPv4Stack" value="true"/>
            <arg value="-startupUrl"/>
            <arg value="Popcornhour_controller.html"/>
            <arg value="org.dyndns.merelin.pchrc.Popcornhour_controller"/>
        </java>
    </target>
</project>
